﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace Kiosk
{
    public partial class frmMain : Form
    {
        public bool FiyatGorAktif { get; set; }
        public bool MusteriGorAktif { get; set; }
        private Form _aktifForm;
        List<Image> _insertImages = new List<Image>();

        public frmMain()
        {
            InitializeComponent();
            axShockwaveFlash1.Movie = Application.StartupPath + "\\movie.swf";
            this.Load += new EventHandler(frmMain_Load);
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            axShockwaveFlash1.Play();
            insertGuncelle();
            fileSystemWatcher1.Path = Application.StartupPath + "\\INSERT";
            fileSystemWatcher1.EnableRaisingEvents = true;
        }

        private void insertGuncelle()
        {
            _insertImages.Clear();
            if (Directory.Exists(Application.StartupPath + "\\INSERT"))
            {
                foreach (string item in Directory.GetFiles(Application.StartupPath + "\\INSERT", "*.jpg"))
                {
                    _insertImages.Add(imageFromFile(item));
                }
            }
            if (_insertImages.Count > 0)
                imgInsert.Image = _insertImages[0];
        }

        private Image imageFromFile(string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            Image img = Image.FromStream(stream);
            stream.Close();
            return img;
        }

        int insertCounter = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (insertCounter == _insertImages.Count)
            {
                insertCounter = 0;
            }

            imgInsert.Image = _insertImages[insertCounter];
            insertCounter++;

            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private bool MesajKontrol()
        {
            bool sonuc = false;
            string filename = Program.Session[Settings.PosKonPath].ToString() + "\\MESAJ." + Convert.ToInt32(Program.Session[Settings.KasaNo]).ToString("000");
            if (File.Exists(filename))
            {
                string posgenel = Program.Session[Settings.PosGenelPath].ToString();
                if (File.ReadAllText(filename).IndexOf("1071") > -1)
                {
                    if (File.Exists(posgenel + "\\URUN.DAT"))
                    {
                        File.Copy(posgenel + "\\URUN.DAT", Application.StartupPath + "\\URUN.DAT", true);
                        sonuc = true;
                    }
                    if (File.Exists(posgenel + "\\TOPTAN.DAT"))
                    {
                        File.Copy(posgenel + "\\TOPTAN.DAT", Application.StartupPath + "\\TOPTAN.DAT", true);
                        sonuc = true;
                    }
                    File.Delete(filename);
                }
            }
            return sonuc;
        }

        private void axShockwaveFlash1_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
        {
            switch (e.command)
            {
                case "fiyatgor":
                    if (MusteriGorAktif)
                    {
                        if (_aktifForm != null)
                            _aktifForm.Close();
                    }

                    if (!FiyatGorAktif)
                    {
                        frmFiyatgor fiyatGor = new frmFiyatgor();
                        fiyatGor.Show(this);
                        fiyatGor.SetDesktopLocation(190, 180);
                        _aktifForm = fiyatGor;
                    }
                    break;
                case "cikis":
                    frmSifre sifre = new frmSifre();
                    if (sifre.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Application.Exit();
                    }
                    break;
                case "musteri":
                    if (FiyatGorAktif)
                    {
                        if (_aktifForm != null)
                            _aktifForm.Close();
                    }
                    if (!MusteriGorAktif)
                    {
                        frmMusteri ms = new frmMusteri();
                        ms.Show(this);
                        ms.SetDesktopLocation(190, 165);
                        _aktifForm = ms;
                    }
                    break;
            }
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                if (MesajKontrol())
                {
                    Database.Get.Yukle();
                }
            }
            catch (Exception)
            {
            }
        }

        private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
        {
            insertGuncelle();
        }

        private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
        {
            insertGuncelle();
        }

        private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
        {
            insertGuncelle();
        }
    }
}
